package jk.spider.plugin.extract.site;

import jk.spider.api.event.SpiderEvent;
import jk.spider.api.event.resource.ResourceFetchedEvent;
import jk.spider.core.SpiderController;
import jk.spider.core.event.CoreEvent;
import jk.spider.mod.plugin.Plugin;
import jk.spider.model.Resource;
import jk.spider.util.config.ConfigConstants;

public class ExtractYHDPlugin implements Plugin {

	public String getDescription() {
		return null;
	}

	public String getName() {
		return null;
	}

	public String getVersion() {
		return null;
	}

	public void initialize() {
		
	}

	public void notifyEvent(SpiderController controller, SpiderEvent event) {
		if(event instanceof ResourceFetchedEvent) {
			Resource tRes = controller.getStorage().getResourceDAO().getResource(event.getResource().getUrl());
			tRes.setBytes(event.getResource().getBytes());
			if(!isExtract(tRes))
				return;
			boolean inData = controller.getSpiderConfiguration().getConfigConfiguration().getBoolean(ConfigConstants.CONFIG_INDATA, false);
			if(inData) {
				ExtractYihaodianToDB ydhToDb = new ExtractYihaodianToDB(controller, tRes);
				ydhToDb.parser();
			} else {
				ExtractYihaodian ydh = new ExtractYihaodian(controller, tRes);
				ydh.parser();
			}
			controller.getStorage().getResourceDAO().setParsered(tRes);
		}
	}

	public void shutdown() {
		
	}
	
	private boolean isExtract(Resource tRes) {
		return (tRes.getType().equals(CoreEvent.COREEVENT_DETAIL) 
				|| tRes.getType().equals(CoreEvent.COREEVENT_MONITOR)
				|| tRes.getType().equals(CoreEvent.COREEVENT_REVIEW))
				&& tRes.getBytes().length > 0;
	}

}
